<?php
/**
 * reference menus
 *
 * @version:	0.1
 * @package:	finances
 * @author:		alex_ez
 * @created:	19.11.2010 23:41
 *
 * @copyright:	(c) 2010 alex <qfox@ya.ru> yaroshevich
 */


class menus_admin_controller extends refbook_controller
{
	protected $entity_title = 'menu item';
	protected $entities_title = 'menu items';
	protected $captions = null;
	protected $item_title_key = 'title';

	// inner lists
	protected $types = array();
	protected $types_short = array();
	protected $states = array();
	protected $states_short = array();

	// form and filter
	protected $pager_per_pages = '10,40,100,400';

	protected $columns = null;
	protected $order = 'url+';

	protected $lists = array( );

	// permissions by roles
	protected $permissions = array(
		'index' => null,
		'add' => null,
		'edit' => null,
		'remove' => null,
		);

	protected $url_patterns = array(
		'index' => '/admin/menus/',
		'show' => '/admin/menus/id:id/',
		'add' => '/admin/menus/add/',
		'edit' => '/admin/menus/edit/?id=:id',
		);

	static public function get_menu_items( ) {
		return array(
			'menus' => array("text" => __("Menu items"), "group" => __('Content data'), 'section' => 'content', "order" => 10),
			);
	}

	protected function init_crumbs() {
		parent::init_crumbs();
	}

	public function on_initialize( &$walker )
	{
		$this->model = new menus();

		$this->types = array(
			'section' => __('Menu section'),
			'item' => __('Menu item'),
			);
		$this->types_short = array(
			'section' => __('M.section'),
			'item' => __('M.item'),
			);
		$this->states = $this->states_short = array(
			'off' => __('Off'),
			'on' => __('On'),
			);

		$this->columns = array(
			'id' => array(
				'text' => __('ID'),
				'width' => '30px',
				'order' => true,
				),
			'tm' => array(
				'text' => __('Creation date'),
				'order' => true,
				'format' => 'datetime',
				),
			'path'	=> __('Path expression'),
			'url'	=> __('URL'),
			'state'	=> __('State'),
			'url'	=> __('URL'),
			'type'	=> __('Type'),
			'title'	=> __('Title'),
			'order'	=> __('Order'),
			);

		$this->captions = array(
			'title'		=> __('Menus'),
			'index'		=> __('Menu items list'),

			'show'		=> __('Show menu item'),
			'add'		=> __('Create menu item'),
			'edit'		=> __('Edit menu item'),
			'remove'	=> __('Remove menu item'),

			'showing'	=> __('Menu item "%s"'),
			'adding'	=> __('New menu item'),
			'editing'	=> __('Modifing menu item "%s"'),
			'removing'	=> __('Deleting menu item "%s"'),
			);

		$this->fieldsets['index#filter'] = array(
			'id' => array( 'type' => 'text', 'label' => __('ID') ),
			'tm' => array( 'label' => __('Creation date'), 'type' => 'date_period' ),
			'title' => array( 'type' => 'text', 'label' => __('Title') ),
			'type' => array( 'type' => 'select', 'label' => __('Type'), 'list' => ':types', ),
			'state' => array( 'type' => 'select', 'label' => __('State'), 'list' => ':states', ),
			);

		parent::on_initialize( $walker );
	}

	public function init_lists()
	{
		$this->lists['types'] = $this->types;
		$this->lists['states'] = $this->states;
	}

	public function get_items( $w = '', $o = null, $limit = null )
	{
		$r = $this->model->by( $w, $o, $limit, -1 );

		foreach( array_keys($r) as $k ) {
			$r[$k]['type'] = $this->types_short[$r[$k]['type']];
			$r[$k]['state'] = $this->states_short[$r[$k]['state']];
		}

		return $r;
	}

}


